package Servlets;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ServiceInterface.ServiceProxy;
import Beans.*;

/**
 * Servlet implementation class SearchForMember
 */
public class SearchForMember extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ServiceProxy proxy = new ServiceProxy();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchForMember() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Member theMemberToSearch = new Member();
		List<Member> memberList;
		String zipFromForm;
		try{
			theMemberToSearch.setAddress(request.getParameter("streetadder"));
			theMemberToSearch.setCity(request.getParameter("city"));
			theMemberToSearch.setEmail(request.getParameter("email"));
			theMemberToSearch.setFirst(request.getParameter("firstname"));
			theMemberToSearch.setLast(request.getParameter("lastname"));
			theMemberToSearch.setState(request.getParameter("state"));
			
			zipFromForm = request.getParameter("zip");
			if(!zipFromForm.equals(""))
				theMemberToSearch.setZip(Integer.parseInt(zipFromForm));
//			else
//				theMemberToSearch
			
			proxy.setEndpoint("http://localhost:8079/VLMS3/services/Service");
			Member[] membersArray = proxy.searchMember(theMemberToSearch);
			memberList = Arrays.asList(membersArray);
			System.out.println("The member is " + membersArray[0].getFirst());
			request.setAttribute("members", memberList);
			request.getRequestDispatcher("ShowMembers").forward(request, response);
		
		}
		catch(Exception e){
			e.printStackTrace();
		}

	}

}
